package com.pybox.common.utils;

import com.pybox.entity.TChangNameDan;
import com.pybox.util.UserUtils;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by wuyingkai on 2018/10/3.
 */
public class ParamUtils {

    public String getParams(StringBuffer sql, Map<String, String> params) {

        String startTime = params.get("startTime");
        String endTime = params.get("endTime");
        String userName = params.get("userName");
        String customerId = UserUtils.getCustomerId().toString();
        String cabinetId = params.get("cabinetId");
        String objectName = params.get("obj_name");
        String event = params.get("event");
        String level = params.get("level");
        String state = params.get("state");

        if (!StringUtils.isEmpty(startTime)) {//只选开始时间
            sql.append(" and l.ctime >= '" + startTime + "'");
        }
        if (!StringUtils.isEmpty(endTime)) {//只选结束时间
            sql.append(" and l.ctime <= '" + endTime + "'");
        }
        if (!StringUtils.isEmpty(userName)) {
            sql.append(" and t.realname like '%" + userName + "%'");
        }
        if (!StringUtils.isEmpty(customerId)) {
            sql.append("  and l.customer_id = '" + customerId + "'");
        }
        if (!StringUtils.isEmpty(cabinetId)) {
            sql.append(" and l.cabinet_Id = " + cabinetId);
        }
        if (!StringUtils.isEmpty(objectName)) {
            sql.append(" and l.obj_name like '%" + objectName.trim() + "%'");
        }
        if (!StringUtils.isEmpty(event)) {
            sql.append(" and l.event =" + event);
        }
        if (!StringUtils.isEmpty(level)) {
            sql.append(" and l.level >=" + level);
        }
        if (!StringUtils.isEmpty(state)) {
            sql.append(" and l.state =" + state);

        }
        sql.append(" order by ctime desc");
        return sql.toString();

    }

    public String getParams(Map<String, String> params, StringBuffer sql) {

        String startTime = params.get("startTime");
        String endTime = params.get("endTime");
        String operate = params.get("operate");
        String userName = params.get("userName");
        String customerId = UserUtils.getCustomerId().toString();

        if (!StringUtils.isEmpty(customerId)) {//只选开始时间
            sql.append(" and customer_id  = '" + customerId + "'");
        }

        if (!StringUtils.isEmpty(startTime)) {//只选开始时间
            sql.append(" and ctime >= '" + startTime + "'");
        }
        if (!StringUtils.isEmpty(endTime)) {//只选结束时间
            sql.append(" and ctime <= '" + endTime + "'");
        }
        if (!StringUtils.isEmpty(operate)) {
            sql.append(" and operate like '%" + operate.trim() + "%'");
        }
        if (!StringUtils.isEmpty(userName)) {
            sql.append(" and realname  like '%" + userName.trim() + "%'");
        }
        sql.append(" order by ctime desc");
        return sql.toString();
    }

    public String getTCabinetsParams(Map<String, String> params, String sql) {
        String isOnline = params.get("isOnline");
        String name = params.get("name");
        String customerId = params.get("customerId");
        String where = "";
        if (!StringUtils.isEmpty(isOnline)) { // 根据离线状态查询
            where += "  and c.is_online = '" + isOnline + "'";
        }
        if (!StringUtils.isEmpty(name)) { // 根据名称查询
            where += "  and c.name like '%" + name + "%'";
        }
        if (!StringUtils.isEmpty(customerId)) { // 根据客户查询
            where += "  and c.customer_id = '" + customerId + "'";
        }
        where = sql + where;
        return where;
    }


    // 钥匙库存统计表参数处理
    public StringBuffer getExportExcelParams(Map<String, String> params, StringBuffer sql) {

        String status = params.get("status");
        String objName = params.get("objName");
        String customerId = UserUtils.getCustomerId().toString();
        if (!StringUtils.isEmpty(status)) { // 根据状态查询
            sql.append("  and o.status = '" + status + "'");
        }
        if (!StringUtils.isEmpty(objName)) { // 根据钥匙名称查询
            sql.append("  and o.name = '" + objName + "'");
        }

        if (!StringUtils.isEmpty(customerId)) { // 根据客户查询
            sql.append("  and o.customer_id = '" + customerId + "'");
        }
        return sql;
    }

    //用户信息查询 参数处理
    public StringBuffer getUserParams(StringBuffer sql, Map<String, String> paramMap) {
        String customerId = paramMap.get("customerId");
        String isAdmin = paramMap.get("isAdmin");
        String realname = paramMap.get("realname");
        if (!StringUtils.isEmpty(isAdmin)) { // 根据用户身份查询
            sql.append("  and u.is_admin = '" + isAdmin + "'");
        }
        if (!StringUtils.isEmpty(realname)) { // 根据名称查询
            sql.append("  and u.realname = '" + realname + "'");
        }

        if (!StringUtils.isEmpty(customerId)) { // 根据客户查询
            sql.append("  and u.customer_id = '" + customerId + "'");
        }
        sql.append("  and u.deleted = '" + 0 + "'");   //0 表示存在   1表示删除

        return sql;
    }

    public StringBuffer getObjectsParams(Map<String, String> params, StringBuffer sql) {

        String name = params.get("name");
        String uuid = params.get("uuid");
//        String category = params.get("category");
//        String color = params.get("color");
//        String day = params.get("day");
//        String hour = params.get("hour");
//        String minute = params.get("minute");
//        String comments = params.get("comments");
//        String deleted = params.get("deleted");
//        String ctime = params.get("ctime");
        String status = params.get("status");
        String customerId = params.get("customerId");

        if (!StringUtils.isEmpty(name)) {
            sql.append("  and o.name like '%" + name.trim() + "%'");
        }
        if (!StringUtils.isEmpty(status)) {
            if (!status.equals("1")) {
                sql.append("  and o.status = 0  or o.status = 2");
            } else {
                sql.append("  and o.status = '" + status + "'");
            }

        }
        if (!StringUtils.isEmpty(customerId)) { // 根据客户查询
            sql.append("  and o.customer_id = '" + customerId + "'");
        }
        if (!StringUtils.isEmpty(uuid)) { // 根据客户查询
            sql.append("  and  o.cabinet_uid = '" + uuid.trim() + "'");
        }
        sql.append(" and o.deleted = 0 ");
        return sql;
    }

    public StringBuffer getTVehicleParams(StringBuffer sql, Map<String, String> paramMap) {
        //SELECT tm.model,tm.Id,tm.color,tm.brand,tm.branch,tm.utime,tm.ctime FROM t_models tm;
        //  String Id = paramMap.get("Id");
        String model = paramMap.get("model");
        String brand = paramMap.get("brand");
        String branch = paramMap.get("branch");
        String customer_id = UserUtils.getCustomerId().toString();

        if (!StringUtils.isEmpty(model)) { // 根据客户查询
            sql.append("  and tm.model = '" + model.trim() + "'");
        }
        if (!StringUtils.isEmpty(customer_id)) { // 根据客户查询
            sql.append("  and tm.customer_id = '" + customer_id + "'");
        }
        if (!StringUtils.isEmpty(brand)) { // 根据客户查询
            sql.append("  and tm.brand = '" + brand.trim() + "'");
        }
        if (!StringUtils.isEmpty(branch)) { // 根据客户查询
            sql.append("  and tm.branch = '" + branch.trim() + "'");
        }
//        if (!StringUtils.isEmpty(Id)) { // 根据客户查询
//            sql.append("  and tm.Id = '" + Id + "'");
//        }
        return sql;
    }

    public StringBuffer getTChangNameDanParams(Map<String, String> params, StringBuffer sql) {


        String startTime = params.get("startTime");
        String endTime = params.get("endTime");
        String t_status = params.get("t_status");
        String beforname = params.get("beforname");
        String reason_id = params.get("reason_id");
        String type = params.get("type");
        int userId = UserUtils.getUserId();

        if (!StringUtils.isEmpty(startTime)) {//只选开始时间
            sql.append(" and t.utime >= '" + startTime + "'");
        }
        if (!StringUtils.isEmpty(endTime)) {//只选结束时间
            sql.append(" and t.utime <= '" + endTime + "'");
        }
        if (!StringUtils.isEmpty(reason_id)) {
            sql.append(" and t.reason_id ='" + reason_id + "'");
        }
        if (!StringUtils.isEmpty(beforname)) {
            sql.append(" and t.beforname like '%" + beforname.trim() + "%'");
        }
        if (!StringUtils.isEmpty(t_status)) {
            sql.append(" and t.t_status  = '" + t_status + "'");
        }
        if (!StringUtils.isEmpty(type)) {
            sql.append(" and t.type  = '" + type + "'");
        }
        sql.append(" and t.user_id  = '" + userId + "'").append(" order by ctime ");
        return sql;
    }

    public StringBuffer getTChangNameDanParams(Integer id, TChangNameDan dto, StringBuffer sql) {
        // UPDATE t_change_name_dan SET model_id =2,vincode6='134561',reason_id=2 WHERE id = 2;
        sql.append("  model_id = '" + dto.getModel_id() + "'").append(",").
                append("  vincode6 = '" + dto.getVincode6() + "'").append(",").
                append(" objectname = '" + dto.getObjectname() + "'").append(",").
                append("  reason_id = '" + dto.getReason_id() + "'").append("WHERE id = '" + id + "'");

        return sql;
    }

    public StringBuffer getTObjectParams(TChangNameDan dto, StringBuffer sql) {


        String time = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        Timestamp utime = Timestamp.valueOf(time);
        //update t_objects set name =?,status=?,vincode6=?,model_id=?
        sql.append("  model_id = '" + dto.getModel_id() + "'").append(",").
                append("  vincode6 = '" + dto.getVincode6() + "'").append(",").
                append("  name = '" + dto.getObjectname() + "'").append(",").
                // append("  status = '" + dto.getReason_id() + "'").append(",").
                        append("  utime = '" + utime + "'").
                append("WHERE id = '" + dto.getObject_id() + "'");
        return sql;
    }
}
